package com.tang.servlet.model;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tang.bean.Book;
import com.tang.bean.Page;
import com.tang.services.BookService;
import com.tang.services.UserService;
import com.tang.services.impl.BookServiceImpl;
import com.tang.servlet.base.ModelBaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "BookManagerServlet")
public class BookManagerServlet extends ModelBaseServlet {
  private BookService bookService= new BookServiceImpl();

  //查询所有的图书列表，并且展示到book_manager.html⻚面
  public void showBookList(HttpServletRequest request, HttpServletResponse response) throws Exception {
    //response.getWriter().write("showBookList...");
    // 查询所有图书列表
    try{
      List<Book> booklist = bookService.getBookList();
      request.setAttribute("bookList", booklist);
      processTemplate("manager/book_manager", request, response);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  //跳转到图书管理⻚面
  public void toBookManagerPage(HttpServletRequest request, HttpServletResponse response) throws IOException{
    processTemplate("manager/book_manager",request,response);
  }

  //删除图书
  public void removeBook(HttpServletRequest request, HttpServletResponse response) throws IOException {
    //1. 获取要删除的图书的id
    Integer id = Integer.valueOf(request.getParameter("id"));
    //2. 调用业务层的方法根据id删除图书
    try {
      bookService.removeBook(id);
      //3. 删除成功，重新查询所有图书信息
      response.sendRedirect(request.getContextPath()+"/bookManager?method=showBookList");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }


  //跳转到添加图书⻚面
  public void toAddPage(HttpServletRequest request, HttpServletResponse response) throws IOException{
    processTemplate("manager/book_edit",request,response);
  }


  //添加或者图书信息
  public void saveOrUpdateBook(HttpServletRequest request, HttpServletResponse response) throws IOException{
    //1. 获取请求参数
    Map<String, String[]> parameterMap = request.getParameterMap();
    //2. 将parameterMap中的数据封装到Book对象
    try {
      Book book = new Book();
      BeanUtils.populate(book,parameterMap);
      bookService.saveOrUpdateBook(book);
      //保存成功，则重新查询所有图书
      response.sendRedirect(request.getContextPath()+"/bookManager?method=showBookList");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }


  //跳转到修改⻚面
  public void toEditPage(HttpServletRequest request, HttpServletResponse response) throws IOException{
    //获取客户端传入的id
    Integer id = Integer.valueOf(request.getParameter("id"));
    try {
      //根据id查询图书详情
      Book book = bookService.getBookById(id);
      //将图书信息存储到请求域
      request.setAttribute("book",book);
      processTemplate("manager/book_edit",request,response);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  //异步分页
  public void pageQuery(HttpServletRequest request,HttpServletResponse response) throws Exception {
    //获取请求的页码
    Integer pageNo;
    if (request.getParameter("pageNo")==null){
      pageNo=1;
    }else{
      pageNo=Integer.valueOf(request.getParameter("pageNo"));
    }
    try{
      //调用业务层的方法执行分页，返回Page对象
      Page<Book> pageList= bookService.getPage(pageNo);
      //将Page对象存储到请求域中
      request.setAttribute("page",pageList);
      //将PageBean对象序列化为json然后返回给html
      ObjectMapper mapper=new ObjectMapper();
      response.setContentType("application/json;charset=utf-8");
      mapper.writeValue(response.getOutputStream(),pageList);

    }catch (Exception e){
      e.printStackTrace();
      //跳转到统一的异常提示页面
      //往request域对象中存放异常信息
      request.setAttribute("errorMsg",e.getMessage());
      processTemplate("error",request,response);
    }
  }

  //价格搜索查询
  public void pageByPrice(HttpServletRequest request,HttpServletResponse response) throws Exception {
    //获取请求的页码
    Integer pageNo,min,max;
    if (request.getParameter("pageNo")==null){
      pageNo=1;
    }else{
      pageNo=Integer.valueOf(request.getParameter("pageNo"));
    }
    if (request.getParameter("min")==null){
      min=0;
    }else{
      min=Integer.valueOf(request.getParameter("min"));
    }
    if (request.getParameter("max")==null){
      max=Integer.MAX_VALUE;
    }else{
      max=Integer.valueOf(request.getParameter("max"));
    }

    try{
      //调用业务层的方法执行分页，返回Page对象
      Page<Book> pageList= bookService.pageByPrice(pageNo,min,max);
      //将Page对象存储到请求域中
      request.setAttribute("page",pageList);
      //将PageBean对象序列化为json然后返回给html
      ObjectMapper mapper=new ObjectMapper();
      response.setContentType("application/json;charset=utf-8");
      mapper.writeValue(response.getOutputStream(),pageList);

    }catch (Exception e){
      e.printStackTrace();
      //跳转到统一的异常提示页面
      //往request域对象中存放异常信息
      request.setAttribute("errorMsg",e.getMessage());
      processTemplate("error",request,response);
    }
  }

  //分类查询
  public void pageByType(HttpServletRequest request,HttpServletResponse response) throws Exception {
    //获取请求的页码
    Integer pageNo;
    String typeId;
    if (request.getParameter("pageNo")==null){
      pageNo=1;
    }else{
      pageNo=Integer.valueOf(request.getParameter("pageNo"));
    }
    //获取分类编号
    typeId=String.valueOf(request.getParameter("typeId"));
    try{
      //调用业务层的方法执行分页，返回Page对象
      Page<Book> pageList= bookService.pageByType(pageNo,typeId);
      //将Page对象存储到请求域中
      request.setAttribute("page",pageList);
      //将PageBean对象序列化为json然后返回给html
      ObjectMapper mapper=new ObjectMapper();
      response.setContentType("application/json;charset=utf-8");
      mapper.writeValue(response.getOutputStream(),pageList);

    }catch (Exception e){
      e.printStackTrace();
      //跳转到统一的异常提示页面
      //往request域对象中存放异常信息
      request.setAttribute("errorMsg",e.getMessage());
      processTemplate("error",request,response);
    }
  }

  //关键字搜索查询
  public void pageByKey(HttpServletRequest request,HttpServletResponse response) throws Exception {
    //获取请求的页码
    Integer pageNo;
    if (request.getParameter("pageNo")==null){
      pageNo=1;
    }else{
      pageNo=Integer.valueOf(request.getParameter("pageNo"));
    }
    //获取关键字
    String key=String.valueOf(request.getParameter("key"));
    try{
      //调用业务层的方法执行分页，返回Page对象
      Page<Book> pageList= bookService.pageByKey(pageNo,key);
      //将Page对象存储到请求域中
      request.setAttribute("page",pageList);
      //将PageBean对象序列化为json然后返回给html
      ObjectMapper mapper=new ObjectMapper();
      response.setContentType("application/json;charset=utf-8");
      mapper.writeValue(response.getOutputStream(),pageList);

    }catch (Exception e){
      e.printStackTrace();
      //跳转到统一的异常提示页面
      //往request域对象中存放异常信息
      request.setAttribute("errorMsg",e.getMessage());
      processTemplate("error",request,response);
    }
  }

  //同步分页
  public void page(HttpServletRequest request,HttpServletResponse response) throws IOException{
    //获取请求的页码
    Integer pageNo;
    String typeId;
    if (request.getParameter("pageNo")==null){
      pageNo=1;
    }else{
      pageNo=Integer.valueOf(request.getParameter("pageNo"));
    }
    //获取分类编号
    typeId=String.valueOf(request.getParameter("typeId"));
    try{
      //调用业务层的方法执行分页，返回Page对象
      Page<Book> pageList=bookService.pageByType(pageNo,typeId);
      //将Page对象存储到请求域中
      request.setAttribute("page",pageList);
      request.setAttribute("typeCode",typeId);
      //跳转到当前页面
      //查询成功，解析Thymeleaf模板
      processTemplate("book_sort02", request, response);

    }catch (Exception e){
      e.printStackTrace();
      //跳转到统一的异常提示页面
      //往request域对象中存放异常信息
      request.setAttribute("errorMsg",e.getMessage());
      processTemplate("error",request,response);
    }
  }
}
